﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Krile.Kernel.Control;
using Krile.Kernel;

namespace Highkick.Dialogs
{
    public partial class HInput : UserControl, IInputMethod
    {
        public Krile.Kernel.Bridges.IInputControlManager ParentICM { get; set; }

        public HInput()
        {
            InitializeComponent();
        }
        private string _imgPath = null;
        public string ImagePath
        {
            get { return _imgPath; }
            set
            {
                _imgPath = value;
                if (String.IsNullOrEmpty(value))
                    imagePath.Text = String.Empty;
                else
                    imagePath.Text = value;
            }
        }

        public MethodInvoker PostCallback{get;set;}

        public MethodInvoker CancelCallback{get;set;}

        public string IdentifyString
        {
            get { return Define.InputMethodId; }
        }

        public string InputText
        {
            get { return inputText.Text; }
            set { inputText.Text = value; }
        }

        public object ExtraData
        {
            get{return imagePath;}
            set
            {
                if (value is string)
                    imagePath.Text = value as string;
            }
        }

        public void Clear()
        {
            this.ImagePath = null;
            this.inputText.Text = String.Empty;
        }

        public void FocusToInput()
        {
            this.inputText.Focus();
        }

        public void Cancelled()
        {
            Clear();
        }

        public bool OnReplyRequired(ulong uid)
        {
            return true;
        }

        private void inputText_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && e.Control)
            {
                e.IsInputKey = true;
            }
        }

        private void inputText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && e.Control)
            {
                e.Handled = true;
                e.SuppressKeyPress = true;
                this.PostCallback.Invoke();
            }
        }

        private void postBtn_Click(object sender, EventArgs e)
        {
            this.PostCallback.Invoke();
        }

        private void attachImage_Click(object sender, EventArgs e)
        {
            using (var ofd = new OpenFileDialog())
            {
                ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
                ofd.Filter = "画像ファイル|*.png;*.jpg;*.jpeg;*.bmp;*.gif|すべてのファイル|*.*";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    ImagePath = ofd.FileName;
                }
            }
        }
    }
}
